import 'package:bruno/bruno.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:yt_devops/configs.dart';
import 'package:yt_devops/model/order_deal_result_model.dart';
import 'package:yt_devops/resources/resources.dart';
import 'package:yt_devops/resources/str_res_keys.dart';
import 'package:yt_devops/services/common/common_services.dart';
import 'package:yt_devops/services/task/task_services.dart';
import 'package:yt_devops/utils/enums.dart';

class FinishTaskController extends GetxController {
  TaskServices services = Get.find();
  CommonServices commonServices = Get.find();

  List<String> get eventNames => events.map((e) => e.name!).toList();

  List<OrderDealResultModel> events = [];
  late int statusType;
  late int id;
  late TaskType type;
  List<String> uploadImageList = [];
  int selectedTagIndex = 0;
  String description = '';

  @override
  void onInit() {
    super.onInit();
    statusType = Get.arguments['status'];
    id = Get.arguments['id'];
    type = Get.arguments['type'];
    getEvents();
  }

  @override
  void onReady() {}

  void onChangehandle(String title) {
    description = title;
  }

  void getEvents() => Configs.httpClient.requestWithLoading(() async {
        var data = await services
            .getInstallDealResults({'id': id, 'status': statusType});
        events =
            List.of(data).map((e) => OrderDealResultModel.fromJson(e)).toList();
        update();
      });

  getCommitService() {
    if (type == TaskType.install) {
      return services.transitInstallTask;
    } else if (type == TaskType.maintenance) {
      return services.transitRepairTask;
    } else {
      return services.transitReportTask;
    }
  }

  String get contentParam {
    if (type == TaskType.install) {
      return 'installationContent';
    } else if (type == TaskType.maintenance) {
      return 'maintenanceContent';
    } else {
      return 'faultDeclarationContent';
    }
  }

  commit() => Configs.httpClient.requestWithLoading(() async {
        var pics = uploadImageList.join(',');
        var service = getCommitService();
        var params = {
          'status': statusType,
          'eventId': events[selectedTagIndex].eventId,
          contentParam: {"id": id, "picture": pics, 'description': description}
        };
        await service(params);

        BrnToast.show(
          ResKey.taskFinishSuccessTip.tr,
          Get.context!,
          preIcon: Image.asset(
            ImageAssets.iconSuccess,
            width: 24,
            height: 24,
          ),
          duration: BrnDuration.short,
          onDismiss: () {
            Get.back(result: true);
          },
        );
      });

  @override
  void onClose() {}
}
